/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package m12cqis01.nhathv.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author NhatHV
 */
public class RegexUtil {

    // Regex to find requirement in prolog result
    public static String REGEX_N_TUHOI = Constants.N_TUHOI + "\\(\\'*([\\w]+\\s*)+\\'*\\)";
    // Regex to find producer in prolog result
    public static String REGEX_N_HSX = Constants.N_HANGSANXUAT + "\\([\\w]+\\s*[\\w]*\\)";
    // Regex to find product name in prolog result
    public static String REGEX_N_SP = Constants.N_SANPHAM + "\\(\\'[\\w]+\\s*[\\w]*\\'\\)";
    // Regex to find product code in prolog result
    public static String REGEX_PRON_SP = Constants.PRON_SP + "\\([\\w]+\\s*[\\w]*\\)";
    // Regex to find properties in prolog result
    public static String REGEX_R_PHUTU = Constants.R_PHUTU + "\\([\\w]*.*\\s*[\\w]*\\'\\)";

    // Regex to find Measure of product
    public static String REGEX_ANSWER_MEASURE = "R?\\d+\\s?[x\\*]\\s?C?\\d+\\s?[x\\*]\\s?S?\\d+";

    // Regex to find Color of product
    public static String REGEX_ANSWER_COLOR = "(màu sắc|mầu sắc|màu|mầu)\\s*[:,]*\\s*(xanh ngọc|đỏ|vàng|cam|xanh|tím|nâu|xám|bạc|hồng|trắng|đen)";

    // Regex to find price of product
    public static String REGEX_ANSWER_PRICE = "(giá bán lẻ đề xuất|giá đề xuất|giá khuyến mãi|giá niêm yết|giá bán lẻ|giá thị trường|giá bán|giá)\\s*:*\\s*(\\d+[\\d,\\s\\.]+)\\s*(đồng|vnđ|vnd|đ|₫)";
    
    // Regex to find Capacity
    public static String REGEX_ANSWER_CAPACITY_TULANH = "(điện năng tiêu thụ|công suất tiêu thụ|công suất)\\s*[:,]*(\\s*\\d+\\s*-*)+\\s*[Ww,]*";

    // Regex to find useless characters
    public static String USELESS_CHAR = "[\\?\\-\\*\\&\\$\\:/]";

    /**
     * Find the specified string in source string by regex.
     *
     * @param source source string.
     * @param regexConstant Regex.
     * @return result of finding.
     */
    public static String findMatch(String source, String regexConstant) {
        String result = "";
        Pattern pattern = Pattern.compile(regexConstant, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(source);
        
        if (matcher.find()) {
            System.out.println("findMatch, matcher = " + matcher.group());
            result = matcher.group();
        }
        return result;
    }
    
    public static void main(String[] args) {
        String prologResult = "s(np(n_tuhoi('ki1ch thu7o71c'), e(cu3a), sp(n_sp('tu3 la5nh'), n(n_hsx(sharp), pron_sp(SJ16VSL)))), vp(r(la2, 'bao nhie6u'))) .";
        findMatch(prologResult, REGEX_N_TUHOI);
        findMatch(prologResult, REGEX_N_HSX);
        findMatch(prologResult, REGEX_N_SP);
        findMatch(prologResult, REGEX_PRON_SP);
        findMatch(prologResult, REGEX_R_PHUTU);
        
        String snippet = "th??ng s??? c???a t??? l???nh SANYO SR-125PN/SS";
        findMatch(snippet, USELESS_CHAR);
        System.out.println("main, replacement: " + snippet.replaceAll(USELESS_CHAR, ""));
    }
}
